﻿using System.Collections.Generic;
using System.Xml;

namespace Quail.Silverlight
{
    /// <summary>
    /// The silverlist ListBox control
    /// </summary>
	public class SlvListBox : SlvControl
	{
        /// <summary>
        /// Initializes a new instance of the <see cref="SlvListBox"/> class.
        /// </summary>
        public SlvListBox()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SlvListBox"/> class.
        /// </summary>
        /// <param name="container">The container for the control.</param>
        /// <param name="selector">The selector.</param>
        public SlvListBox(SlvControlContainer container, string selector)
            : base(container, selector)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SlvListBox"/> class.
        /// </summary>
        /// <param name="control">The control.</param>
        public SlvListBox(SlvControl control)
            : base(control)
        {
        }

        /// <summary>
        /// Gets or sets the currently selected index
        /// </summary>
        public int SelectedIndex
        {
            get { return int.Parse(GetPropertyValue("SelectedIndex")); }
            set { SetPropertyValue("SelectedIndex", value.ToString()); }
        }

        /// <summary>
        /// Gets the Items count
        /// </summary>
        public int ItemsCount
        {
            get { return int.Parse(GetPropertyValue("ItemsCount")); }
        }

        /// <summary>
        /// Gets or sets the current value
        /// </summary>
        public string Value
        {
            get { return GetPropertyValue("Value"); }
            set { SetPropertyValue("Value", value); }
        }

        /// <summary>
        /// Gets the count of checkboxes
        /// </summary>
        public int CheckBoxValuesCount
        {
            get { return int.Parse(GetPropertyValue("CheckBoxValues.length")); }
        }

        /// <summary>
        /// Gets a list of checkbox values
        /// </summary>
        public List<SlvCheckBox> CheckBoxValues
        {
            get
            {
                var checkList = new List<SlvCheckBox>();
                for (var i = 0; i < CheckBoxValuesCount; i++)
                {
                    checkList.Add(new SlvCheckBox(this, "checkbox:eq(" + i + ")"));
                }

                return checkList;
            }
        }

        /// <summary>
        /// Gets an array of values from the list
        /// </summary>
    	public string[] Values
    	{
    		get
    		{
    		    return GetPropertyValueArray("Values");
    		}
    	}

        /// <summary>
        /// Gets the array of values as an Xml string
        /// </summary>
		public XmlDocument ValuesAsXml
		{
			get
			{
				var dom = new XmlDocument();

			    dom.LoadXml(GetPropertyValue("ValuesAsXml"));
				
				return dom;
			}
		}
    }
}
